home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
cool
/
ge_cool.lha
/
GE_COOL2.1
/
man
/
oldman3
/
Hash_Table.3T
< prev
next >
Wrap
Text File
|
1992-06-26
|
10KB
|
320 lines
.TH HASH_TABLE
.SH NAME
Hash_Table<Ktype,Vtype>\f1 A dynamic, parameterized hash table
.SH SYNOPSIS
#include <cool/Hash_Table.h>
.SH DESCRIPTION
The \f3Hash_Table<Ktype,VType>\f1 class is publicly derived from the
Hash_Table
class and implements hash tables of user-specified types for both the key and
the value. This is accomplished by using the parameterized type capability of
C++ .
The
Hash_Table
class is dynamic in nature. Its size (that is, the number
of buckets in the table) is always a prime number. Each bucket holds eight
items. No holes are left in a bucket; if a key/value pair is removed from the
middle of a bucket, the following entries are moved up. When a hash on a key
ends up in a bucket that is full, the table is enlarged.
.SH Base Classes
Hash_Table,
.SH Friend Classes
None
.SH Constructors
.TP
\f3Hash_Table<Ktype,Vtype> ();\f1
Allocates a hash table of the default size (three buckets).
.TP
\f3Hash_Table<Ktype,Vtype> \f3(unsigned long \f2number\f3);\f1
Allocates a hash table with at least enough buckets for
number
entries.
.TP
\f3Hash_Table<Ktype,Vtype> (const Hash_Table<Ktype,Vtype>& ht\f3);\f1
Duplicates the size and entries of another hash table object
ht .
.SH Member Functions
.TP
inline long capacity () const;
Returns the maximum number of entries that the hash table can hold.
.TP
void clear ();
Removes all entries from the hash table and adjusts the appropriate counts.
.TP
inline Hash_Table_state& current_position () const;
Returns a reference to the state information associated with the current
position. This function should be used with the \f3Iterator<Type>\f1 class to save
and restore the current position, thus facilitating multiple iterators over an
instance of hash table.
.TP
\f3Boolean find (const Ktype& key\f3);\f1
Searches the hash table for
key
and returns
TRUE
if found; otherwise, this
function returns
FALSE .
If
key
is found, this function sets the current
position to the key/value entry; otherwise, this function invalidates the
current position.
.TP
\f3Boolean get (const Ktype& key, Vtype& value\f3);\f1
Calculates the hash value for
key
and returns the value associated with that
key in the table by copying it to
value .
This function sets the current
position to the key/value pair. If
key
is found, this function returns
TRUE ;
otherwise, this function returns
FALSE .
.TP
inline long get_bucket_count () const;
Returns the prime number of buckets currently allocated for the hash table.
.TP
\f3inline int get_count_in_bucket (long \f2n\f3) const;\f1
Returns the number of keys currently hashed to the zero-relative nth bucket.
.TP
\f3Boolean get_key (const Vtype& value, Ktype& key\f3);\f1
Searches the table for
value .
If found, this function copies the associated key
into
key ,
sets the current position to the key/value pair, and returns
TRUE .
If
value
is not found in the hash table, this function invalidates the current
position and returns
FALSE .
.TP
inline Boolean is_empty () const;
Returns
TRUE
if the hash table contains no entries; otherwise, this function
returns
FALSE .
.TP
\f3const Ktype& \f3key ();\f1
Returns the key of the key/value pair at the current position. If the current
position is invalid, an
\f3\f3Error\f1\f1
exception is raised.
.TP
inline long length () const;
Returns the number of entries in the hash table.
.TP
Boolean next ();
Advances the current position pointer to the next entry in the hash table and
returns
TRUE .
If the current position is invalid, this function advances to the
first entry and returns
TRUE .
If advancing past the last entry in the hash
table, this function invalidates the current position and returns
FALSE .
.TP
\f3Hash_Table<Ktype,Vtype>& \f3operator= (const Hash_Table<Ktype,Vtype>& ht\f3);\f1
Overloads the assignment operator for the class and assigns one hash table
object to have the value of another by duplicating the size and entries. This
function invalidates the current position of the object.
.TP
\f3Boolean operator== (const Hash_Table<Ktype,Vtype>& ht\f3);\f1
Overloads the equality operator for the hash table class. This function returns
TRUE
if the tables have the same number of buckets with the same key/value
pairs; otherwise, this function returns
FALSE .
.TP
\f3inline Boolean operator!= (const Hash_Table<Ktype,Vtype>& ht\f3);\f1
Overloads the inequality operator for the hash table class. This function
returns
TRUE
if the tables have a different number of buckets or different
key/value pairs; otherwise, this function returns
FALSE .
.TP
Boolean prev ();
Moves the current position pointer to the previous entry in the hash table and
returns
TRUE .
If the current position is invalid, this function moves to the
last entry and returns
TRUE .
If moving to the previous entry passes the first
entry in the hash table, this function invalidates the current position and
returns
FALSE .
.TP
\f3Boolean put (const Ktype& key\f3, const Vtype& value\f3);\f1
Searches the hash table for
key
and puts the corresponding key/value pair into
the hash table. If
key
is not there, the key/value pair is added and
TRUE
is returned; otherwise, if
key
is already there, this function updates the value
with
value
and returns
FALSE .
If the bucket determined by the hash is full, the
table grows and the key/value pairs are rehashed and inserted. This function
sets the current position to the key/value pair.
.TP
Boolean remove ();
Removes the key/value at the current position and returns
TRUE .
This function
sets the current position to the entry immediately following the entry removed
if in the same bucket; otherwise, this function invalidates the current
position. If the current position is invalid, an
\f3\f3Error\f1\f1
exception is raised and,
if the handler returns, this function returns
FALSE .
.TP
\f3Boolean remove (const Ktype& key\f3);\f1
Searches the hash table for
key ,
removes the indicated key/value pair from the
table, sets the current position to the old location of the key/value pair, and
returns
TRUE .
If key is not found in the hash table, this function returns
FALSE .
.TP
inline void reset ();
Invalidates the current position.
.TP
\f3Boolean resize (long \f2number\f3);\f1
Resizes the hash table for at least the indicated number of entries. If a
growth ratio has been selected and it satisfies the resize request, the table
is grown by this ratio. This function invalidates the current position. If the
resize value is zero or negative, an
\f3\f3Error\f1\f1
exception is raised.
.TP
\f3inline void set_hash (\f2Hash h\f3);\f1
Updates the hash function for this instance of hash table.
Hash
is a function
of type
unsigned long
(\f2*Function\f1) (\f3const Ktype&\f1). If the key is of type
char* ,
the hash is the result of successively exclusive-or-ing each byte with the
current hash value shifted left seven bits. If the key is not of type
char* ,
the default hash function is the computation of a 32-bit value shifted left
three bits with the result then modulo the prime number of buckets. If the size
of \f2(Ktype)\f1 is greater than four bytes, the 32-bit value is computed by
successively exclusive-or-ing 32-bit values for the length of the key.
.TP
\f3void set_key_compare (\f2Hash_Key_Compare \f3= NULL);\f1
Updates the key compare function for this instance of hash table.
Hash_Key_Compare
is a function of type
Boolean
(\f2*Function\f1)(\f3const Ktype&\f1, \f3const Ktype&\f1). If no argument is provided, the
operator==
for
Ktype ,
the key over
which the class is parameterized, is used. If the key is a
char* ,
a
String ,
or a
Gen_String ,
the default compare function is a string comparison.
.TP
\f3inline void set_ratio (\f2float\f3);\f1
Updates the growth ratio for this instance of the hash table to the specified
value. When a hash table needs to grow, the current size is multiplied by the
ratio to determine the new size. If
ratio
is negative, an
\f3\f3Error\f1\f1
exception is raised.
.TP
\f3void set_value_compare (\f2Hash_Value_Compare \f3= NULL);\f1
Updates the value compare function for this instance of hash table.
Hash_Value_Compare
is a function of type
Boolean
(\f2*Function\f1)(\f3const Vtype&\f1, \f3const Vtype&\f1). If no argument is provided, the
operato==
for
Vtype ,
the value over
which the class is parameterized, is used.
.TP
\f3const Vtype& \f3value ();\f1
Returns a reference to the value of the key/value pair at the current position.
If the current position is invalid, an
\f3\f3Error\f1\f1
exception is raised.
.SH Friend Functions
.TP
\f3friend ostream& operator<< (ostream& \f2os\f3, const Hash_Table<Ktype,Vtype>& ht\f3);\f1
Overloads the output operator for a reference to a
Hash_Table< Ktype,Vtype >
object. This function provides a formatted output with key/value pairs printed
one per line.
.TP
\f3inline friend ostream& operator<< (ostream& \f2os\f3, const Hash_Table<Ktype,Vtype>* ht\f3);\f1
Overloads the output operator for a pointer to a
Hash_Table< Ktype,Vtype >
object. This function provides a formatted output with key/value pairs printed
one per line.
.SH COPYRIGHT
Copyright (C) 1991 Texas Instruments Incorporated.
Permission is granted to any individual or institution to use, copy, modify,
and distribute this software, provided that this complete copyright and
permission notice is maintained, intact, in all copies and supporting
documentation.
Texas Instruments Incorporated provides this software "as is" without
express or implied warranty.